//
//  CarDetailViewController.m
//  Rail
//
//  Created by hychen on 2018/3/4.
//  Copyright © 2018年 Hychen. All rights reserved.
//

#import "CarDetailViewController.h"
#import "CarImageTableViewCell.h"
#import "LineDetailDescTableViewCell.h"
#import "LineDetailLongDescTableViewCell.h"
#import "ButtonTableViewCell.h"
#import "ESPictureBrowser.h"

#import "WarnLevelConfig.h"

#import "CarLogOutViewController.h"

@interface CarDetailViewController () <ESPictureBrowserDelegate>
@property (nonatomic, strong) ESPictureBrowser *browser;
@property (nonatomic, weak) NSArray *selectImageArray;
@end

@implementation CarDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.navigationItem addSimpleTitleView:@"车辆详情"];
    
    self.isOpenHeader = NO;

    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([LineDetailLongDescTableViewCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([LineDetailLongDescTableViewCell class])];
    
    [self setupData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)setupData {
    NSString *outTime = nil;
    if (self.model.status == CarLogTypeLogIn) {
        outTime = @"未离岗";
    }
    else {
        outTime = [BMUtils chineseYYYYMMDDHHmmWithServiceTime:self.model.out_time];
    }
    
    NSMutableArray *inImageArray = [NSMutableArray array];
    if ([self.model.in_img1 objectIsNotEmpty]) {
        [inImageArray addObject:self.model.in_img1];
    }
    if ([self.model.in_img2 objectIsNotEmpty]) {
        [inImageArray addObject:self.model.in_img2];
    }
    
    NSMutableArray *outImageArray = [NSMutableArray array];
    if ([self.model.out_img1 objectIsNotEmpty]) {
        [outImageArray addObject:self.model.out_img1];
    }
    if ([self.model.out_img2 objectIsNotEmpty]) {
        [outImageArray addObject:self.model.out_img2];
    }
    
    self.dataArray = @[
                       
                       @{@"type":@"string",@"title":@"登记工区",@"data":self.model.area_name?:@""},
                       @{@"type":@"string",@"title":@"登记时间",@"data":[BMUtils chineseYYYYMMDDHHmmWithServiceTime:self.model.record_time]?:@""},
                       @{@"type":@"string",@"title":@"进岗时间",@"data":[BMUtils chineseYYYYMMDDHHmmWithServiceTime:self.model.in_time]?:@""},
                       @{@"type":@"string",@"title":@"离岗时间",@"data":outTime?:@""},
                       @{@"type":@"string",@"title":@"车型类型",@"data":[WarnLevelConfig carTypeStringWithType:self.model.car_type]},
                       @{@"type":@"string",@"title":@"车牌号码",@"data":self.model.plate_num?:@""},
                       @{@"type":@"string",@"title":@"车主姓名",@"data":self.model.driver_name?:@""},
                       @{@"type":@"string",@"title":@"联系手机",@"data":self.model.mobile?:@""},
                       @{@"type":@"string",@"title":@"驾驶证号",@"data":self.model.driver_no?:@""},
                       @{@"type":@"image",@"title":@"驾驶证照",@"data":@[self.model.driver_cert?:@""]},
                       @{@"type":@"image",@"title":@"进岗照片",@"data":inImageArray},
                       @{@"type":@"text",@"title":@"备注信息:",@"data":self.model.note?:@"无"},
                       ].mutableCopy;
    
    if (outImageArray.count > 0) {
        [self.dataArray insertObject:@{@"type":@"image",@"title":@"离岗照片",@"data":outImageArray} atIndex:self.dataArray.count-1];
    }
    
    if (self.model.status == CarLogTypeLogIn) {
        [self.dataArray addObject:@{@"type":@"button",@"title":@"车辆离岗",@"data":@""}];
    }
    
    [self.tableView reloadData];
}

//离岗成功更新数据
- (void)refreshData {
    BMWeakSelf
    [HttpRequest loadCarAccessListDataWithQueryDate:@"" pageNum:@"1" areaId:self.model.areaid?:@"" plateNum:self.model.plate_num?:@"" success:^(NSArray *object) {
        if (object.count > 0) {
            CarAccessModel *model = object.firstObject;
            if ([model isKindOfClass:[CarAccessModel class]]) {
                weakSelf.model = model;
                [weakSelf setupData];
            }
        }
    } failure:^(NSString *code, NSString *msg) {
        
    }];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSDictionary *dict = [self.dataArray objectAtIndex:indexPath.row];
    if ([[dict objectForKey:@"type"] isEqualToString:@"string"]) {
        return 55.f;
    }
    else if ([[dict objectForKey:@"type"] isEqualToString:@"image"]) {
        return 160.f;
    }
    else if ([[dict objectForKey:@"type"] isEqualToString:@"text"]) {
        CGFloat height = [tableView fd_heightForCellWithIdentifier:NSStringFromClass([LineDetailLongDescTableViewCell class]) configuration:^(LineDetailLongDescTableViewCell *cell) {
            cell.title = [dict objectForKey:@"title"];
            cell.content = [dict objectForKey:@"data"];
        }];
        return height;
    }
    else if ([[dict objectForKey:@"type"] isEqualToString:@"button"]) {
        return 95.f;
    }
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSDictionary *dict = [self.dataArray objectAtIndexSafe:indexPath.row];
    
    if ([[dict objectForKey:@"type"] isEqualToString:@"string"]) {
        LineDetailDescTableViewCell *cell = [LineDetailDescTableViewCell cellWithTableView:tableView class:[LineDetailDescTableViewCell class]];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.titleLabel.text = [dict objectForKey:@"title"];
        cell.contentLabel.text = [dict objectForKey:@"data"];
        return cell;
    }
    else if ([[dict objectForKey:@"type"] isEqualToString:@"image"]) {
        CarImageTableViewCell *cell = [CarImageTableViewCell cellWithTableView:tableView class:[CarImageTableViewCell class]];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.title = [dict objectForKey:@"title"];
        cell.imageArray = [dict objectForKey:@"data"];
        BMWeakSelf
        [cell setBlockClickImage:^(NSInteger index,NSArray *imageArray) {
            weakSelf.selectImageArray = imageArray;
            [weakSelf.browser showFromView:weakSelf.tableView picturesCount:imageArray.count currentPictureIndex:index];
        }];
        return cell;
    }
    else if ([[dict objectForKey:@"type"] isEqualToString:@"text"]) {
        //备注信息
        LineDetailLongDescTableViewCell *cell = [LineDetailLongDescTableViewCell cellWithTableView:tableView class:[LineDetailLongDescTableViewCell class]];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.title = [dict objectForKey:@"title"];
        cell.content = [dict objectForKey:@"data"];
        return cell;
    }
    else if ([[dict objectForKey:@"type"] isEqualToString:@"button"]) {
        ButtonTableViewCell *cell = [ButtonTableViewCell cellWithTableView:tableView class:[ButtonTableViewCell class]];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.buttonTitle = [dict objectForKey:@"title"];
        BMWeakSelf
        [cell setBlockClickEventButton:^{
            [weakSelf requestCarLogOut];
        }];
        
        return cell;
    }

    return [UITableViewCell new];
}

//车辆离岗
- (void)requestCarLogOut {
    CarLogOutViewController *vc = [CarLogOutViewController new];
    vc.model = self.model;
    [self.navigationController pushViewController:vc animated:YES];
}

- (NSString *)pictureView:(ESPictureBrowser *)pictureBrowser highQualityUrlStringForIndex:(NSInteger)index {
    return [self.selectImageArray objectAtIndexSafe:index];
}

- (ESPictureBrowser *)browser {
    if (!_browser) {
        _browser = [[ESPictureBrowser alloc] init];
        _browser.delegate = self;
    }
    return _browser;
}
@end
